<template>
  <div>
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="130px">
      <div class="com-form-title">基本信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" readonly :disabled="disableForm || optType !== 'add'" @click.native="optType === 'add' ? showStoreModal = true : null">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="年度月份信息" prop="yearMonthInfo">
            <!-- <el-input v-model="form.yearMonthInfo" :disabled="disableForm" /> -->
            <el-date-picker v-model="form.yearMonthInfo" style="width:100%" type="month" value-format="yyyy-MM" :disabled="disableForm" />

          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="本月计划支付金额" prop="paymentAmount">
            <el-input v-model="form.paymentAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="本月公司核定金额" prop="approvedAmount">
            <el-input v-model="form.approvedAmount" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="8">
          <el-form-item label="上报状态" prop="reportStatus">
            <el-input v-model="form.reportStatus" :disabled="disableForm" />
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>

      <div style="padding-bottom: 10px;">
        <el-button type="primary" :disabled="disableForm" @click="addRow()">新增</el-button>
        <el-button type="primary" :disabled="disableForm" @click="delDetailDataItem()">删除</el-button>
      </div>
      <com-table
        v-model="selectRow"
        highlight-current-row
        :data="targetList"
        :columns="columns"
        :no-page="true"
        row-key="sequence"
      >
        <template v-slot:sequence="{row}">
          <el-input v-model="row.sequence" :disabled="disableForm" />
        </template>
        <template v-slot:category="{row}">
          <el-input v-model="row.category" :disabled="disableForm" />
        </template>
        <template v-slot:payObject="{row}">
          <el-input v-model="row.payObject" :disabled="disableForm" />
        </template>
        <template v-slot:payContent="{row}">
          <el-input v-model="row.payContent" :disabled="disableForm" />
        </template>
        <template v-slot:planAmount="{row}">
          <el-input v-model="row.planAmount" :disabled="disableForm" />
        </template>
        <template v-slot:approvedAmount="{row}">
          <el-input v-model="row.approvedAmount" :disabled="disableForm" />
        </template>
        <template v-slot:remark="{row}">
          <el-input v-model="row.remark" :disabled="disableForm" />
        </template>

      </com-table>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
  </div>
</template>

<script>
import { addProjectFundPlanInfo, updateWorkflowProjectFundPlanInfo, getProjectFundPlanInfoDetail } from '@/api/moneyManage'
import { getAreaList } from '@/api/common'
import { workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'

export default {
  name: 'AddProjectFundPlanInfo',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      detailDataItem: {
        type: ''
      },
      detailRules: {
        type: [
          { required: true, message: '请输入类别名称', trigger: 'change' }
        ]
      },
      showAddDetailModal: false,
      showStoreModal: false,
      showFileModal: false,
      selectRow: null,
      workflowStatus,
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        projectName: '',
        yearMonthInfo: '',
        paymentAmount: '',
        approvedAmount: '',
        remark: '',
        reportStatus: ''
      },
      columns: [
        {
          label: '序号',
          prop: 'sequence',
          slot: 'sequence',
          width: '80'
        },
        {
          label: '类别',
          prop: 'category',
          slot: 'category'
        },
        {
          label: '支付对象',
          prop: 'payObject',
          slot: 'payObject'
        },
        {
          label: '支付内容',
          prop: 'payContent',
          slot: 'payContent'
        },
        {
          label: '计划金额(元)',
          prop: 'planAmount',
          slot: 'planAmount'
        },
        {
          label: '核定金额(元)',
          prop: 'approvedAmount',
          slot: 'approvedAmount'
        },
        {
          label: '备注',
          prop: 'remark',
          slot: 'remark'
        }
      ],
      targetList: [{
        sequence: '一',
        category: '分包付款',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: ''
      },
      {
        sequence: '二',
        category: '付材料款',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: ''
      },
      {
        sequence: '三',
        category: '付机械费',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: ''
      },
      {
        sequence: '四',
        category: '付其他',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: ''
      },
      {
        sequence: '五',
        category: '付管理费',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: ''
      },
      {
        sequence: '六',
        category: '周转资金',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: ''
      }],
      // columns: [
      //   {
      //     label: '项目资金计划信息Id',
      //     prop: 'projectFundPlanInfoId'
      //   },
      //   {
      //     label: '序号',
      //     prop: 'sequence'
      //   },
      //   {
      //     label: '类别',
      //     prop: 'category'
      //   },
      //   {
      //     label: '支付对象',
      //     prop: 'payObject'
      //   },
      //   {
      //     label: '支付内容',
      //     prop: 'payContent'
      //   },
      //   {
      //     label: '计划金额',
      //     prop: 'planAmount'
      //   },
      //   {
      //     label: '核定金额',
      //     prop: 'approvedAmount'
      //   },
      //   {
      //     label: '备注',
      //     prop: 'remark'
      //   },
      //   {
      //     label: '编制时间',
      //     prop: 'createTime',
      //     slot: 'createTime'
      //   },

      //   {
      //     fixed: 'right',
      //     prop: 'options',
      //     slot: 'options'
      //   }
      // ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        projectName: [
          { required: true, message: '项目名称', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    data: {
      handler(newData, oldData) {
        if (newData.id) {
          this.getDocDetail(newData.businessId)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    cancelAddDetail() {
      this.showAddDetailModal = false
    },
    addDetail() {
      this.$forceUpdate()
      this.showAddDetailModal = false
    },
    addRow() {
      if (!this.selectRow) {
        this.$message({ type: '提示', message: '请选中一行' })
      }
      const index = this.targetList.indexOf(this.selectRow)
      const row = {
        sequence: '',
        category: '',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: ''
      }
      this.targetList.splice(index + 1, 0, row)
    },
    delDetailDataItem() {
      if (this.selectRow) {
        const index = this.targetList.indexOf(this.selectRow)
        this.targetList.splice(index, 1)
      }
    },
    getStoreSelectedData(type, data) {
      if (type === 'store') {
        this.form.projectName = data.contractName
      }
    },
    initTableData() {
      this.targetList = [{
        index: 0,
        sequence: '一',
        category: '分包付款',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: '',
        children: [
          {
            sequence: '1',
            category: this.detailDataItem.type,
            payObject: '',
            paycontent: '',
            planAmount: '',
            approvedAmount: '',
            remark: ''

          }
        ]
      },
      {
        index: 1,
        sequence: '二',
        category: '付材料款',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: '',
        children: []
      },
      {
        index: 2,
        sequence: '三',
        category: '付机械费',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: '',
        children: []

      },
      {
        index: 3,
        sequence: '四',
        category: '付其他',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: '',
        children: []

      },
      {
        index: 4,
        sequence: '五',
        category: '付管理费',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: '',
        children: []

      },
      {
        index: 5,
        sequence: '六',
        category: '周转资金',
        payObject: '',
        paycontent: '',
        planAmount: '',
        approvedAmount: '',
        remark: '',
        children: []
      }]
    },
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getProjectFundPlanInfoDetail({ projectFundPlanInfoId: id }).then(res => {
        const { lstAttachment, projectFundPlanInfo, lstProjectFundPlanDetail } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        Object.keys(this.form).forEach(key => {
          const source = projectFundPlanInfo[key]
          if (typeof source === 'boolean') {
            this.form[key] = source ? 1 : 0
          } else {
            this.form[key] = source
          }
        })
        this.targetList = lstProjectFundPlanDetail
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },

    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            projectFundPlanInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList,
            lstProjectFundPlanDetail: this.targetList
          }
          const fn = this.optType === 'add' ? addProjectFundPlanInfo : updateWorkflowProjectFundPlanInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer{
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .el-form-item__label{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;
}
::v-deep .el-input.is-disabled .el-input__inner{
  color: #000;

}
</style>
